// pages/detail/index.js
var WxParse = require('../../wxParse/wxParse');
import {
  articleInfo,
  collect,
  delcollect,
  setLikeCount
} from '../../utils/api';
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    info: {},
    articleId: '',
    typeName: '',
    list: [],
    imgUrl: app.globalData.imgUrl,
    wxImgUrl: app.globalData.wxImgUrl,
    tagBg: app.globalData.wxImgUrl + '文章标签@3x.png',
    navBarHeight: '', //头高度
    statusBarHeight: '', //状态栏高度
    statusLickCount: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取头部高度
    let navBarHeightinfo = app.publicFun.navBarHeightinfo()
    this.setData({
      navBarHeight: navBarHeightinfo[0],
      statusBarHeight: navBarHeightinfo[1]
    })
    this.getArticleInfo(options.id)
  },

  back() {
    wx.navigateBack({})
  },

  // 文章信息
  async getArticleInfo(id) {
    try {
      wx.showLoading({
        title: '加载中...',
      });
      const res = await articleInfo({
        id
      })
      if (res) {
        console.log(res)
        wx.hideLoading();
        res.createTime = res.createTime.substring(0, 10);
        let typeName = res.categories[0].name;
        this.setData({
          info: res,
          typeName: typeName,
          statusLickCount:res.likeWithMe
        });
        WxParse.wxParse('article', 'html', res.content, this, 5);
      }
    } catch (error) {}
  },
  handlerHome() {
    wx.switchTab({
      url: '/pages/index/index',
    });
  },
  handleFavour() {
    this.lickCount()
  },
  handlerCollet(e) {
    if (wx.getStorageSync('token') != '') {
      let i = e.currentTarget.dataset.id;
      if (i == 0) {
        collect({
          articleId: this.data.info.id,
          userId: wx.getStorageSync('userid'),
        }).then(res => {
          if (res.code == undefined) {
            wx.showToast({
              title: '收藏成功',
              icon: 'none',
            });
            this.data.info.collectWithMe = true;
            this.setData({
              info: this.data.info,
            });
          } else {
            wx.showToast({
              title: res.message,
              icon: 'none',
            });
          }
        });
      } else {
        delcollect({
          articleId: this.data.info.id,
          userId: wx.getStorageSync('userid'),
        }).then(res => {
          if (res.code == undefined) {
            wx.showToast({
              title: '取消成功',
              icon: 'none',
            });
            this.data.info.collectWithMe = false;
            this.setData({
              info: this.data.info,
            });
          } else {
            wx.showToast({
              title: res.message,
              icon: 'none',
            });
          }
        });
      }
    } else {
      this.selectComponent('#modeShow').modeShow(); // 通过实例调用组件事件
    }
  },
  // 点赞计数接口
  async lickCount() {
    if (wx.getStorageSync('token') == '') {
      this.selectComponent('#modeShow').modeShow(); // 通过实例调用组件事件
      return
    }
    try {
      const res = await setLikeCount({
        id: this.data.info.id
      });

      if (res) {
        this.setData({
          statusLickCount: res.result
        })
        this.getArticleInfo(this.data.info.id)
      }
    } catch (error) {
      console.log(error)
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: this.data.info.title,
    };
  },
});